/**
 * Copyright 2010 (c) Jack Pien
 */

#ifndef __DDXX_COMMON_H
#define __DDXX_COMMON_H

#ifdef __cplusplus
extern "C" {
#endif

#define DD_ASCII_DPKT 0x544B5044
#define DD_ASCII_D 0x44
#define DD_ASCII_P 0x50
#define DD_ASCII_K 0x4b
#define DD_ASCII_T 0x54
#define DD_PACKET_MIN_BYTE_SIZE 14 // 4 (hdr), 4 (ts), 4 (msgbytes), 2 (crc)
#define DD_MSG_MAX_BYTE_SIZE 114
#define DD_PACKET_MAX_BYTE_SIZE (DD_MSG_MAX_BYTE_SIZE + DD_PACKET_MIN_BYTE_SIZE)

#define DD_PACKET_BYTE_OFFSET_HDR 0
#define DD_PACKET_BYTE_SIZE_HDR 4

#define DD_PACKET_BYTE_OFFSET_SENDERTS 4
#define DD_PACKET_BYTE_SIZE_SENDERTS 4

#define DD_PACKET_BYTE_OFFSET_MSGBYTES 8
#define DD_PACKET_BYTE_SIZE_MSGBYTES 4

#define DD_PACKET_BYTE_OFFSET_MSG 12

#define DD_PACKET_BYTE_SIZE_CRC 2

// Packet defined
typedef struct DDpacket_t {
    uint32_t hdr;
    uint32_t senderTs;
    uint32_t msgBytes;
    uint8_t* msgPtr;
    uint16_t crc;
} DDpacket;

// Packet routines
uint16_t ddComputeCrc16( uint8_t* buf, uint32_t nbytes);
uint32_t ddPackHeader( uint32_t ts, uint32_t msgBytes, uint8_t* buf );
uint32_t ddPack( uint32_t ts, uint32_t msgBytes, 
                 const uint8_t* msg, uint8_t* buf );
boolean ddUnpackNoMsgCopy( uint8_t* buf, DDpacket* pkt );
uint32_t ddUnpackPeekHdrPacketSize( const uint8_t* buf );


#ifdef __cplusplus
}
#endif


#endif // __DDXX_COMMON_H

